# -*- coding: utf-8 -*-
"""
@Time ： 2024/3/13 9:34
@Auth ： gc
"""
import akshare as ak
import pandas as pd
import matplotlib.pyplot as plt
import datetime

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False    # 用来正常显示负号
def choice_stock(codes = None):
    choice_stock_of_histy_25_list = list()
    if codes is None:
        codes = pd.read_csv("stock/data/hs300_stocks.csv", converters = {"code":str})["code"].values.tolist()
    for code in codes:
        stock_individual_info_em_df = ak.stock_individual_info_em(symbol=code)
        stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
        stock_info = stock_zh_a_spot_em_df[stock_zh_a_spot_em_df["代码"] == code].to_dict('records')[0]
        start = dict(zip(stock_individual_info_em_df["item"].values.tolist(),stock_individual_info_em_df["value"].values.tolist()))["上市时间"]
        end = datetime.datetime.now().strftime("%Y%m%d")
        start = stock_zh_a_hist = ak.stock_zh_a_hist(symbol=code, period="daily", start_date=start, end_date=end, adjust="")
        stock_zh_a_hist_qfq = ak.stock_zh_a_hist(symbol=code, period="daily", start_date=start, end_date=end, adjust="qfq")
        stock_zh_a_hist_hfq = ak.stock_zh_a_hist(symbol=code, period="daily", start_date=start, end_date=end, adjust="hfq")
        describe = stock_zh_a_hist["收盘"].describe()
        real_sfw = stock_zh_a_hist["收盘"][stock_zh_a_hist["收盘"] < describe["75%"]].describe()["25%"]
        real_sfw_qfq = stock_zh_a_hist_qfq["收盘"][stock_zh_a_hist_qfq["收盘"] < describe["75%"]].describe()["25%"]
        real_sfw_hfq = stock_zh_a_hist_hfq["收盘"][stock_zh_a_hist_hfq["收盘"] < describe["75%"]].describe()["25%"]
        if stock_info["最新价"]<=real_sfw:
            stock_info.update({"real_sfw":real_sfw,"real_sfw_qfq":real_sfw_qfq,"real_sfw_hfq":real_sfw_hfq})
            choice_stock_of_histy_25_list.append(stock_info)
            print(stock_info)
    pd.DataFrame(choice_stock_of_histy_25_list).to_csv("stock/data/choice_stock_of_histy_25.csv",index=False)
